package com.heba.helper;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import com.heba.Debug;

import java.io.File;

/**
 * Created by shenghao on 2015/6/10.
 */
public class TakePhotoHelper {

    public static final int TAKE_PICTURE = 0;
    public static final int CHOOSE_PICTURE = 1;
    public static final int PICTURE_SEND = 2;

    /**
     * 拍照
     *
     * @param activity
     * @param tempFile
     */
    public static void takePicture(Activity activity, File tempFile) {
        Intent cameraintent = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);
        // 指定调用相机拍照后照片的储存路径
        cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
        activity.startActivityForResult(cameraintent, TAKE_PICTURE);
    }

    /**
     * 相册
     *
     * @param activity
     */
    public static void selectPhoto(Activity activity) {
        //另一种方式,调用系统相册,然后再裁剪，解决miui8的裁剪问题
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                "image/*");   //相片类型
        activity.startActivityForResult(intent, CHOOSE_PICTURE);
    }
    /**
     * 拍照后裁剪图片
     *
     * @param activity
     * @param tempFile
     * @param uri
     * @param output
     * @param requestCode
     */
    public static void cropImage16and9(Activity activity, File tempFile, Uri uri, int output, int requestCode) {
        Debug.Log("cropImage", "裁剪图片");
        // 裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // 裁剪框的比例，1：1
        intent.putExtra("aspectX", 16);
        intent.putExtra("aspectY", 9);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", output);
        intent.putExtra("outputY", output/16*9);
        // 图片格式
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        //return false 以免图片过大小米系统崩溃
        intent.putExtra("return-data", false);
        //tempFile为文件保存地址
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
        activity.startActivityForResult(intent, requestCode);
    }
    /**
     * 拍照后裁剪图片
     *
     * @param activity
     * @param tempFile
     * @param uri
     * @param output
     * @param requestCode
     */
    public static void cropImage(Activity activity, File tempFile, Uri uri, int output, int requestCode) {
        Debug.Log("cropImage", "裁剪图片");
        // 裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // 裁剪框的比例，1：1
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", output);
        intent.putExtra("outputY", output);
        // 图片格式
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        //return false 以免图片过大小米系统崩溃
        intent.putExtra("return-data", false);
        //tempFile为文件保存地址
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
        activity.startActivityForResult(intent, requestCode);
    }
    public static void saveOldImage(Activity activity, File tempFile, Uri uri, int output, int requestCode) {
        Debug.Log("cropImage", "裁剪图片");
        // 裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 图片格式
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        //return false 以免图片过大小米系统崩溃
        intent.putExtra("return-data", false);
        //tempFile为文件保存地址
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
        activity.startActivityForResult(intent, requestCode);
    }
}
